function isWeixin() {
  const agent = navigator.userAgent.toLowerCase();
  const math = agent.match(/MicroMessenger/i)
  if (!math) {
    return false
  }
  if (math === 'micromessenger' || math.includes('micromessenger')) {
    return true
  }
  return false
}
function getShortLink() {
  const search = new URLSearchParams(location.search)
  const vin = search.get('No')
  if (!vin) {
    alert('桩号异常，请重试')
    return
  }
  const params = {
    vin,
    carBrand: '/pages/stubScan/index',
    carModel: 'release',
  }
  fetch('https://homechargesmall.liaoqianapp.com/app/wechat/config', {
    method: 'POST',
    body: JSON.stringify(params),
    headers: {
      'Content-type': 'application/json;charset=UTF-8'
    },
  }).then(res => {
    if (res.ok) {
      return res.json()
    }
  }).then(res => {
    if (res.code === '200') {
      const btn = document.getElementById('btn')
      btn.innerText = res.data
      window.location.href = res.data
      setTimeout(() => {
        WeixinJSBridge.call('closeWindow')
        window.close()
      }, 2000)
    } else {
      alert(res.text)
    }
  })
}

(function() {
  if (isWeixin()) {
    getShortLink()
  } else {
    alert('请使用微信扫码打开本页面')
  }
})()